package com.cl.unipay4j.wx.pay;

import com.cl.unipay4j.core.*;
import com.cl.unipay4j.core.pay.IQuery;
import com.cl.unipay4j.core.pay.PayQueryModel;
import com.cl.unipay4j.core.pay.PayResponse;
import com.cl.unipay4j.core.util.EnumUtil;
import com.cl.unipay4j.core.util.StrUtil;
import com.cl.unipay4j.wx.AbstractWxPay;
import com.cl.unipay4j.wx.sdk.WXPayConstants;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 *支付宝结果查询
 *该接口提供所有微信支付订单的查询，商户可以通过该接口主动查询订单状态，完成下一步的业务逻辑。
 *
 * 需要调用查询接口的情况：
 *
 * ◆ 当商户后台、网络、服务器等出现异常，商户系统最终未接收到支付通知；
 * ◆ 调用支付接口后，返回系统错误或未知交易状态情况；
 * ◆ 调用被扫支付API，返回USERPAYING的状态；
 * ◆ 调用关单或撤销接口API之前，需确认支付状态；
 * @Author Small
 * @Date 2019/11/27 18:56
 * @Version 1.0
 **/
@Slf4j
public class WxPayQuery extends AbstractWxPay implements IQuery {

    public WxPayQuery() {
        super();
    }

    public WxPayQuery(IPayConfig payConfig) {
        super(payConfig);
    }

    /**
     * 方法描述:  查询支付结果
     * @Author Small
     * @param model
     * @Date 2020/4/25 11:24
     * @return com.cl.unipay.core.QueryResult
     */
    @Override
    public Result<PayResponse> execQuery(PayQueryModel model) {
        log.info("WxQuery::=>订单{} 请求到达", model.getOrderNo());
        try {
            // 查询订单状态
            Map<String, String> map = new HashMap<>();
            if (StrUtil.isNotEmpty(model.getTradeNo())){
                map.put("transaction_id",model.getTradeNo());
            }
            if (StrUtil.isNotEmpty(model.getOutTradeNo())){
                map.put("out_trade_no",model.getOutTradeNo());
            }
//            wxPayConfig.setTradeType(EnumUtil.getIEnum(WxTradeTypeEnum.class,model.getTradeType()));
            wxPayConfig.setTradeType(WxTradeTypeEnum.valueOf(model.getTradeType()));
            Map<String, String> r = initWxPay().orderQuery(map);
            log.info("WxQuery::=>订单{} 返回报文，{}", model.getOrderNo(), r);
            String returnCode = r.get(WXPayConstants.RETURN_CODE);
            if (UnipayConst.SUCCESS.equals(returnCode)) {
                if (UnipayConst.SUCCESS.equals(r.get(WXPayConstants.RESULT_CODE))) {
                    TradeStateEnum stateEnum = EnumUtil.getIEnum(WxTradeStateEnum.class, r.get(WXPayConstants.TRADE_STATE)).getTradeState();
                    if (TradeStateEnum.SUCCESS.equals(stateEnum)) {
                        return Result.tradeSuccess(wrapperPayResponse(r));
                    } else {
                        return Result.tradeFail(stateEnum.getValue(), stateEnum.getDesp());
                    }
                } else {
                    String errCode = r.get(WXPayConstants.ERR_CODE);
                    String errCodeDes = r.get(WXPayConstants.ERR_CODE_DES);
                    log.info("WxQuery::=>订单{} 查询失败,错误代码{},错误原因{}", model.getOrderNo(), errCode, errCodeDes);
                    return Result.returnFail(errCode, errCodeDes);
                }
            } else {
                String returnMsg = r.get(WXPayConstants.RETURN_MSG);
                log.info("WxQuery::=>订单{} 查询失败,错误代码{},错误原因{}", model.getOrderNo(), returnCode, returnMsg);
                return Result.returnFail(returnCode, returnMsg);
            }

        } catch (Exception e) {
            log.warn("WxQuery=>订单{} 查询时发生异常", model.getOrderNo(), e);
        }
        return Result.exception();
    }


    public static void main(String[] args) {
        System.out.println(WxTradeTypeEnum.valueOf("XCX"));
    }

}
